package com.hunan.yllxy.util;

import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @Author: 刘华
 * @Date: 2024/3/29 0029 15:05
 * @Version 1.0
 */
public class TempPathUtil {

    public static String getTempPath() {
        String property = System.getProperty("user.dir");
        String uuid = UUID.randomUUID().toString();
        String dirPath = property + File.separator+ "tempPath" +  File.separator + uuid;
        File file = new File(dirPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return dirPath;
    }

    public static File getWordTemplate(String wordName,String parentPath) {
        ClassPathResource classPathResource = new ClassPathResource("word"+ File.separator+"template" +  File.separator + wordName);
        InputStream inputStream = null;
        String tempName = UUID.randomUUID().toString() + ".docx";
        File tempFile=null;
        try {

            inputStream = classPathResource.getInputStream();
             tempFile = new File(parentPath + "\\" + tempName);
            if (!tempFile.exists()) {
                tempFile.createNewFile();
            }
            FileUtils.copyToFile(inputStream, tempFile);
            /**
             * 注册word 去除水印
             */
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (inputStream!=null){
                    inputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return tempFile;
    }


    public static InputStream getResourceFile(String fileName) {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        InputStream inputStream = null;
        try {
            inputStream = classPathResource.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return inputStream;
    }
}
